<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs>
          <el-tab-pane label="组织架构"></el-tab-pane>
        </el-tabs>

        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="19">
            <svg-icon icon-class="bank" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="5">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" default-expand-all>
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!-- 
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="19">
                <svg-icon icon-class="person1" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="5">
                <el-row type="flex" justify="space-between">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="doAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="doEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          :disabled="data.children.length !== 0"
                          @click.native="doDel(data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <el-dialog
        :visible.sync="dialogVisible"
        :title="isEdit ? '编辑部门' : '新增部门'"
        width="50%"
      >
        <deptDialog
          v-if="dialogVisible"
          :cur-add-dept-id="curAddDeptId"
          :is-edit="isEdit"
          :simple-list="simpleList"
          @addSuccess="addSuccess"
          @cancelAdd="cancelAdd"
        ></deptDialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from "@/api/departments";
import { arrayToTree } from "@/utils/index";
import deptDialog from "./deptDialog.vue";

export default {
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 当前添加部门的上属部门id
      curAddDeptId: "",
      dialogVisible: false,
      isEdit: false,
      simpleList: [],
    };
  },
  components: { deptDialog },

  created() {
    this.loadDepts();
  },

  methods: {
    async loadDepts() {
      const res = await getDepartments();
      console.log(res);
      // res.data.depts.splice(0, 1);
      this.list = arrayToTree(res.data.depts, "");
      this.simpleList = res.data.depts.map((item) => {
        return {
          name: item.name,
          code: item.code,
          id: item.id,
          pid: item.pid,
        };
      });
    },
    doAdd(id) {
      this.dialogVisible = true;
      this.isEdit = false;
      console.log(id);
      this.curAddDeptId = id;
    },
    addSuccess() {
      this.dialogVisible = false;
      this.loadDepts();
    },
    doEdit(id) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.curAddDeptId = id;
    },
    cancelAdd() {
      this.addSuccess();
    },
    async delDept(id) {
      try {
        const res = await delDepartment(id);
        this.$message.success(res.message);
        this.loadDepts();
      } catch (err) {
        // console.dir(err);
        this.$message.error(err.message);
      }
    },
    doDel(id) {
      this.$confirm("此操作将永久删除数据,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除成功
          this.delDept(id);
        })
        .catch(() => {
          // 取消删除
        });
    },
  },
};
</script>
